package com.mianyang.zhangsan.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mianyang.zhangsan.common.base.BaseController;
import com.mianyang.zhangsan.common.base.R;
import com.mianyang.zhangsan.model.dto.SecPaperCorrectDto;
import com.mianyang.zhangsan.model.dto.SecPaperDto;
import com.mianyang.zhangsan.model.dto.SecPracticeDto;
import com.mianyang.zhangsan.model.dto.SecUserAnswer;
import com.mianyang.zhangsan.model.entity.SecPaper;
import com.mianyang.zhangsan.model.entity.SecPaperInst;
import com.mianyang.zhangsan.service.SecPaperInstService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author jiangjr
 * @date 2022-12-01
 */
@RestController
@RequestMapping("/practice")
public class SecPracticeController extends BaseController {
    @Autowired
    private SecPaperInstService paperInstService;


    /**
     * 查询试卷下的练习
     */
    @GetMapping("/query/{paperId}")
    public R queryPracticeByPaper(@PathVariable("paperId") Long paperId) {
        SecPracticeDto practices = paperInstService.selectPractice(paperId);
        return success("practices", practices);
    }

    /**
     * 生成练习
     */
    @GetMapping("/generator/{paperId}")
    public R generatorPractice(@PathVariable("paperId") Long paperId) {
        SecPaperDto practice = paperInstService.generatorPractice(paperId);
        return success("practice", practice);
    }

    /**
     * 查看最近练习
     */
    @GetMapping("/recent/{top}")
    public R queryRecentExam(@PathVariable("top") int top) {
        List<SecPaperInst> papers = paperInstService.selectPaperOrderBy("create_time", top);
        return success("papers", papers);
    }

    /**
     * 查询练习
     */
    @GetMapping("/query")
    public R queryPractice(SecPaperInst paper,
                           @RequestParam(value = "current", defaultValue = "1") Integer current,
                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SecPaperInst> practices = paperInstService.selectPracticeByPage(paper, current, pageSize);
        return success("practices", practices);
    }

    /**
     * 删除练习
     */
    @DeleteMapping("/delete/{paperInstId}")
    public R deletePractice(@PathVariable("paperInstId") Long paperInstId) {
        paperInstService.removeById(paperInstId);
        return success();
    }

    /**
     * 查看练习
     */
    @GetMapping("/show/{paperInstId}")
    public R showPractice(@PathVariable("paperInstId") Long paperInstId) {
        SecPaperDto paper = paperInstService.selectPracticeInfo(paperInstId);
        return success("paper", paper);
    }

    /**
     * 再次练习
     */
    @GetMapping("/repeat/{paperInstId}")
    public R practiceRepeat(@PathVariable("paperInstId") Long paperInstId) {
        SecPaperDto paper = paperInstService.practice(paperInstId);
        return success("paper", paper);
    }

    /**
     * 批改试卷
     */
    @PostMapping("/correct")
    public R correctPaper(@RequestBody SecUserAnswer userAnswer) {
        int score = paperInstService.correct(userAnswer);
        return success("score", score);
    }

    /**
     * 删除试卷
     */
    @DeleteMapping("/delete/paper/{paperId}")
    public R deletePaper(@PathVariable("paperId") Long paperId) {
        paperInstService.deleteAndPaperInst(paperId);
        return success();
    }
}
